<?php
namespace app\admin\controller;

class Deal extends Base
{
    private $obj;

    public function _initialize(){
        $this->obj = model('Deal');
    }

    public function index()
    {
        $data = input('get.');
        // var_dump($data);die;
        $sdata = [];

        if(!empty($data['start_time']) && !empty($data['end_time']) && strtotime($data['start_time']) < strtotime($data['end_time'])){
            $sdata['create_time'] = [
                ['gt',strtotime($data['start_time'])],
                ['lt',strtotime($data['end_time'])],
            ];
        }

        if(!empty($data['category_id'])){
            $sdata['category_id'] = $data['category_id'];
        }
        if(!empty($data['city_id'])){
            $sdata['city_id'] = $data['city_id'];
        }


        if(!empty($data['name'])){
            $sdata['name'] = ['like','%'.$data['name'].'%'];
        }

        //获取待审和通过的购数据
        $sdata['status'] = ['gt',-1];

        $deals = $this->obj->getDeals($sdata);
        $deals = new \Page($deals,15);

        $cityArrs = $categoryArrs = [];
        $citys = model('city')->getCitysByParentId(['gt',0]);
        foreach ($citys as $city) {
            $cityArrs[$city->id] = $city->name;
        }
        $categorys = model('category')->getCategorysByParentId();
        foreach ($categorys as $category) {
            $categoryArrs[$category->id] = $category->name;
        }
        // var_dump($cityArrs);die;
        $this->assign([
            'citys' => $citys,
            'categorys' => $categorys,
            'deals' => $deals,
            'category_id' => empty($data['category_id']) ? '' : $data['category_id'],
            'city_id' => empty($data['city_id']) ? '' : $data['city_id'],
            'name' => empty($data['name']) ? '' : $data['name'],
            'start_time' => empty($data['start_time']) ? '' : $data['start_time'],
            'end_time' => empty($data['end_time']) ? '' : $data['end_time'],
            'cityArrs' => $cityArrs,
            'categoryArrs' => $categoryArrs,
        ]);
        return $this->fetch();
    }
}
